/*
 * @Author: 一盏青灯
 * @Date: 2021-02-07 16:10:34
 * @LastEditTime: 2021-02-08 14:34:41
 * @Description: 弹出框模块
 * @FilePath: \flutter_study\lib\pages\popup_box.dart
 */

import 'package:flutter/material.dart';

class PopUpBoxDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("PopUpBox"),
        centerTitle: true,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          // SimpleDialogDemo(),
          // SizedBox(height: 10.0),
          // AlertDialogDemo(),
          // SizedBox(height: 10.0),
          SnackBarDemo(),
        ],
      ),
    );
  }
}

// SimpleDialog 显示对话框
class SimpleDialogDemo extends StatefulWidget {
  @override
  _SimpleDialogDemoState createState() => _SimpleDialogDemoState();
}

class _SimpleDialogDemoState extends State<SimpleDialogDemo> {
// 按钮点击事件
  _clickThisBtn() {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: Text("标题"),
            children: <Widget>[
              // Container(
              //   width: 200.0,
              //   height: 100.0,
              //   alignment: Alignment.center,
              //   child: Text("主体内容"),
              // ),
              SimpleDialogOption(
                child: Text('option A'),
                onPressed: () {},
              ),
              SimpleDialogOption(
                child: Text('option B'),
                onPressed: () {},
              ),
              SimpleDialogOption(
                child: Text('option C'),
                onPressed: () {},
              ),
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: SizedBox(
        width: 200.0,
        height: 40.0,
        child: RaisedButton(
          child: Text('SimpleDialog 显示对话框'),
          color: Colors.pink,
          textColor: Colors.white,
          onPressed: _clickThisBtn,
        ),
      ),
    );
  }
}

// 提示对话框
class AlertDialogDemo extends StatefulWidget {
  @override
  _AlertDialogDemoState createState() => _AlertDialogDemoState();
}

class _AlertDialogDemoState extends State<AlertDialogDemo> {
  // 点击事件
  _clickThisBtn() {
    // print("AlertDialog is open");
    showDialog(
        context: context,
        // barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text("标题"),
            content: Text("Are you sure this ?"),
            actions: <Widget>[
              FlatButton(
                child: Text("cancel"),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              FlatButton(
                child: Text("ok"),
                onPressed: () {
                  Navigator.pop(context);
                },
              )
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: SizedBox(
        width: 200.0,
        height: 40.0,
        child: RaisedButton(
          child: Text('AlertDiaLog 提示对话框'),
          color: Colors.pink,
          textColor: Colors.white,
          onPressed: _clickThisBtn,
        ),
      ),
    );
  }
}

// 操作提示对话框
class SnackBarDemo extends StatefulWidget {
  @override
  _SnackBarDemoState createState() => _SnackBarDemoState();
}

class _SnackBarDemoState extends State<SnackBarDemo> {
  _clickThisBtn() {
    Scaffold.of(context).showSnackBar(SnackBar(
      content: Text("新增成功！"),
      action: SnackBarAction(
        label: 'ok',
        onPressed: () {},
      ),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: SizedBox(
        width: 200.0,
        height: 40.0,
        child: RaisedButton(
          child: Text('SnackBar 操作提示对话框'),
          color: Colors.pink,
          textColor: Colors.white,
          onPressed: _clickThisBtn,
        ),
      ),
    );
  }
}
